/**
 * 集成日志输出工具类
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */

package ext.generic.erp.util;

import java.io.Serializable;

import org.apache.log4j.Logger;

import ext.generic.erp.config.ERPPropertiesUtil;

import wt.log4j.LogR;
import wt.method.RemoteAccess;

public class PDMIntegrationLogUtil implements RemoteAccess, Serializable {

	private static final long serialVersionUID = 1849548443252074612L;
	
	private static final String CLASSNAME = PDMIntegrationLogUtil.class.getName() ;

	private static Logger logger = LogR.getLogger(CLASSNAME);
	
	public static boolean verbose = true ;
	private static int log_level = 0 ;
	
	static {
		try {
//			verbose = (WTProperties.getLocalProperties()).getProperty( PDMIntegrationLogConstant.VERBOSE , false);			
		} catch (Throwable throwable) {
			printLog( CLASSNAME , throwable.getMessage() ,IntegrationConstant.LOG_LEVEL_ERROR ) ;
			throw new ExceptionInInitializerError(throwable);
		}
		
		try{
			String log_level_str = (String)ERPPropertiesUtil.getInstance().readProperties().get("log_level");
			log_level = Integer.parseInt(log_level_str);
		}catch(Exception e){
			log_level = 0 ;
			printLog( CLASSNAME , e.getMessage(),IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}finally{
			logger.info("Current Log Level : " + log_level );
		}
	}
	
	/**
	 * 
	 * 一般情况下，日志等级为INFO，即为1级
	 * 
	 * @param clazz
	 * @param logInfo
	 */
	public static void printLog( Class<?> clazz , String logInfo ){
		printLog( clazz , logInfo , IntegrationConstant.LOG_LEVEL_INFO ) ;
	}
	
	/**
	 * 一般情况下，日志等级为INFO，即为1级
	 * 
	 * @param clazz
	 * @param logInfo
	 */
	public static void printLog( String clazz , String logInfo ){
		printLog( clazz , logInfo , IntegrationConstant.LOG_LEVEL_INFO ) ;
	}
	
	/**
	 * 
	 * @param clazz
	 * @param logInfo
	 * @param level
	 */
	public static void printLog( Class<?> clazz, String logInfo , int level ){
		printLog( clazz , logInfo , level , verbose) ;
	}
	
	/**
	 * 
	 * @param clazz
	 * @param logInfo
	 * @param level
	 */
	public static void printLog( String clazz , String logInfo , int level){
		printLog( clazz , logInfo , level , verbose) ;
	}
	
	/**
	 * 
	 * @param clazz
	 * @param logInfo
	 * @param level
	 * @param verbose
	 */
	public static void printLog(Class<?> clazz , String logInfo , int level , boolean verbose){
		if( clazz != null ){
			printLog( clazz.getName() , logInfo , level , verbose) ;
		}
	}
	
	/**
	 * 当日志开关verbose=true时，
	 * 只输出日志等级大于和等于当前设定等级log_level的日志
	 * 
	 * @param clazz
	 * @param logInfo
	 * @param level
	 * @param verbose
	 */
	public static void printLog( String clazz , String logInfo , int level , boolean verbose){
		
		if(level >= log_level){
			
			if( verbose ){
				if(logInfo == null || logInfo.trim().equals("")){
					logInfo = "" ;
				}

				logger = LogR.getLogger( clazz ) ;
				
				if( level == IntegrationConstant.LOG_LEVEL_INFO ){
					logger.info( logInfo );
				}else if( level == IntegrationConstant.LOG_LEVEL_WARN ){
					logger.warn( logInfo );
				}else if( level == IntegrationConstant.LOG_LEVEL_ERROR ){
					logger.error( logInfo );
				}else{
					logger.debug( logInfo );
				}
			}
		}
	}
	
	/**
	 * Test Case必须嵌入到工作流中，才可以输出到指定的日志文件中。
	 * Windchill Shell中，不会输出到指定的日志文件中。
	 * 
	 */
	public static void testCase() {
		System.out.println("Test Log Start......") ;
		
		logger.debug("Debug log......") ;
		
		printLog( CLASSNAME , "Debug Log" , 0 ) ;
		
		printLog( CLASSNAME , "Add Log" ) ;
		
		printLog( CLASSNAME , "WARNing Log" , 2 ) ;
		
		printLog( CLASSNAME , "Error Log" , 3) ;
		
		System.out.println("Test Log End......") ;
	}
	
	public static void main(String[] args) {
		
		testCase();
	}
}
